+function($){
  viewinit();
  function viewinit(){
    $.get('/shop/getallshop', function(data) {
      var json = JSON.parse(data);
      var $shop = $('#shopid');
      for (var i = 0; i < json.length; i++) {
        $shop.append('<option value="'+json[i].shopid+'">'+json[i].shopname+'</option>')
      }
    });
    $.get('/brand/getallbrand', function(data) {
      var json = JSON.parse(data);
      var $brand = $('#bid');
      for (var i = 0; i < json.length; i++) {
        $brand.append('<option value="'+json[i].bid+'">'+json[i].bname+'</option>')
      }
    });
  }
	$('body').on('click', '#teadd', function(event) {
		  var flag = true;
      $('input:not([type=button]),.YYYY,.MM,.DD,#bid,#shopid').each(function(index, el) {
        if($(this).val()==''){
          $(this).css('border', '3px solid #f77');
					flag = false;
          alert('请将信息补充完整');
					return false;
        }
        $(this).attr('style', ' ');
      });
			if(flag == false){
				return false;
			}
      if(window.shopcount.length==0){
        alert('请选择店铺');
        $('.selectshop').css('border', '3px solid #f77');
        return false;
      }
      $('.selectshop').attr('style','');
      if(window.brandcount.length==0){
        alert('请选择品牌');
        $('.selectbrand').css('border', '3px solid #f77');
        return false;
      }
      $('.selectbrand').attr('style','');
      var tebirth =   $('select[name="birth-YYYY"]').val()+'-'+$('select[name="birth-MM"]').val()+'-'+$('select[name="birth-DD"]').val();
      var shopid = '';
      var bid = '';
      var shopname = '';
      var bname = '';
      for (var i = 0; i < window.shopcount.length; i++) {
        if(window.shopcount[i]){
          shopid = shopid+','+window.shopcount[i];
        }
      }
      for (var i = 0; i < window.brandcount.length; i++) {
        if(window.brandcount[i]){
          bid = bid+','+window.brandcount[i];
        }
      }
      for (var i = 0; i < window.shopnamecount.length; i++) {
        if(window.shopnamecount[i]){
          shopname = shopname+','+window.shopnamecount[i];
        }
      }
      for (var i = 0; i < window.brandnamecount.length; i++) {
        if(window.brandnamecount[i]){
          bname = bname+','+window.brandnamecount[i];
        }
      }
      shopid = shopid.replace(',','');
      bid = bid.replace(',','');
      shopname = shopname.replace(',','');
      bname = bname.replace(',','');
      var postdata = {
  				tename: $('#tename').val(),
  				tetel: $('#tetel').val(),
  				tebirth: tebirth,
  				shopid: shopid,
  				bid: bid,
  				shopname: shopname,
  				bname: bname,
  				teteacher: $('#teteacher').val()
      }
			$.post('/terminal/addTerminal', postdata, function(data) {
				if (data != 'false') {
					alert('添加成功');
				} else {
					alert('添加失败');
				}
			});
	});
}(jQuery);
